<template>
  <div>
    <!-- 表单 -->
    <el-form :label-position="labelPosition" size="small" :model="form" :inline="true">
      <el-form-item label="姓名" style="font-weight:bold;">
        <el-input v-model="form.name" placeholder="请输入姓名" style="width:200px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="部门" style="font-weight:bold;">
        <el-select v-model="form.deptId" placeholder="请选择部门" style="width:200px;" clearable>
          <el-option v-for="item in DepartmentName" :key="item.id" :label="item.departName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工种" style="font-weight:bold;">
        <el-select v-model="form.typeId" placeholder="请选择工种" style="width:200px;" clearable>
          <el-option label="副经理" value="1"></el-option>
          <el-option label="主管" value="2"></el-option>
          <el-option label="班长" value="3"></el-option>
          <el-option label="电工" value="4"></el-option>
          <el-option label="瓦工" value="5"></el-option>
          <el-option label="水工" value="6"></el-option>
          <el-option label="木工" value="7"></el-option>
          <el-option label="综合" value="8"></el-option>
          <el-option label="水电工" value="9"></el-option>
          <el-option label="水电木工" value="10"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
        @click="getDataList">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>
    <!-- 表单 -->

    <!-- 按钮 -->
    <el-row>
      <el-button size="small" type="primary" @click="dialog_newly = true" icon="el-icon-folder-add">新增</el-button>
      <el-button size="small" style="margin-left:10px;" type="success" plain icon="el-icon-download"
        @click="exportMaintainer">导出维修工信息</el-button>
    </el-row>
    <!-- 按钮 -->

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" border style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column prop="workerNum" label="工号" align="center"></el-table-column>
      <el-table-column prop="workerName" label="姓名" align="center"></el-table-column>
      <el-table-column prop="sex" label="性别" align="center"></el-table-column>
      <el-table-column prop="phone" label="电话" align="center"></el-table-column>
      <el-table-column prop="campusName" label="校区" align="center"></el-table-column>
      <el-table-column prop="dept" label="部门" align="center"></el-table-column>
      <el-table-column prop="typeId" label="工种" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.typeId == 1">副经理</span>
          <span v-if="scope.row.typeId == 2">主管</span>
          <span v-if="scope.row.typeId == 3">班长</span>
          <span v-if="scope.row.typeId == 4">电工</span>
          <span v-if="scope.row.typeId == 5">瓦工</span>
          <span v-if="scope.row.typeId == 6">水工</span>
          <span v-if="scope.row.typeId == 7">木工</span>
          <span v-if="scope.row.typeId == 8">综合</span>
          <span v-if="scope.row.typeId == 9">水电工</span>
          <span v-if="scope.row.typeId == 10">水电木工</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0">在职</span>
          <span v-if="scope.row.state == 1">离职</span>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateMaintainer(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteMaintainer({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>


    <!-- 添加维修工弹窗 -->
    <el-dialog title="添加维修工" :visible.sync="dialog_newly">
      <el-form :model="formNew" :label-position="labelPosition" label-width="90px" style="margin-left:10%;"
        :inline="true">
        <el-form-item label="姓名 :" :rules="[{ required: true, message: '姓名不能为空' }]" prop="workerName">
          <el-input v-model="formNew.workerName" style="width:200px;" placeholder="请输入姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号 :" :rules="[{ required: true, message: '账号不能为空' }]" prop="workerNum">
          <el-input v-model="formNew.workerNum" style="width:200px;" placeholder="请输入账号" clearable></el-input>
        </el-form-item>
        <el-form-item label="性别 :" :rules="[{ required: true, message: '性别不能为空' }]" prop="sex">
          <el-select v-model="formNew.sex" placeholder="请选择性别" style="width:200px;" clearable>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号 :" :rules="[{ required: true, message: '身份证号不能为空' }]" prop="idLast">
          <el-input v-model="formNew.idLast" style="width:200px;" placeholder="请输入身份证号后六位" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码 :" :rules="[{ required: true, message: '密码不能为空' }]" prop="password">
          <el-input v-model="formNew.password" style="width:200px;" placeholder="请输入密码" clearable></el-input>
        </el-form-item>
        <el-form-item label="校区 :">
          <el-select v-model="formNew.campusId" placeholder="请选择校区" style="width:200px;" clearable>
            <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门 :">
          <el-select v-model="formNew.deptId" placeholder="请选择部门" style="width:200px;" clearable>
            <el-option v-for="item in DepartmentName" :key="item.id" :label="item.departName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号 :" :rules="[{ required: true, message: '手机号不能为空' }]" prop="phone">
          <el-input v-model="formNew.phone" style="width:200px;" placeholder="请输入手机号" clearable></el-input>
        </el-form-item>
        <el-form-item label="工种 :" :rules="[{ required: true, message: '工种不能为空' }]" prop="typeId">
          <el-select v-model="formNew.typeId" placeholder="请选择工种" style="width:200px;" clearable>
            <el-option label="副经理" value="1"></el-option>
            <el-option label="主管" value="2"></el-option>
            <el-option label="班长" value="3"></el-option>
            <el-option label="电工" value="4"></el-option>
            <el-option label="瓦工" value="5"></el-option>
            <el-option label="水工" value="6"></el-option>
            <el-option label="木工" value="7"></el-option>
            <el-option label="综合" value="8"></el-option>
            <el-option label="水电工" value="9"></el-option>
            <el-option label="水电木工" value="10"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addMaintainer" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加维修工弹窗 -->

    <!-- 修改维修工弹窗 -->
    <el-dialog title="修改维修工" :visible.sync="dialog_change">
      <el-form :model="formChange" :label-position="labelPosition" label-width="90px" style="margin-left:10%;"
        :inline="true">
        <el-form-item label="姓名 :">
          <el-input v-model="formChange.workerName" style="width:200px;" placeholder="请输入姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号 :">
          <el-input v-model="formChange.workerNum" style="width:200px;" placeholder="请输入账号" clearable></el-input>
        </el-form-item>
        <el-form-item label="性别 :">
          <el-select v-model="formChange.sex" placeholder="请选择性别" style="width:200px;" clearable>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号 :">
          <el-input v-model="formChange.idLast" style="width:200px;" placeholder="请输入身份证号后六位" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码 :">
          <el-input v-model="formChange.password" style="width:200px;" placeholder="请输入新密码" clearable></el-input>
        </el-form-item>
        <el-form-item label="校区 :">
          <el-select v-model="formChange.campusId" placeholder="请选择校区" style="width:200px;" clearable>
            <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门 :">
          <el-select v-model="formChange.deptId" placeholder="请选择部门" style="width:200px;" clearable>
            <el-option v-for="item in DepartmentName" :key="item.id" :label="item.departName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号 :">
          <el-input v-model="formChange.phone" style="width:200px;" placeholder="请输入手机号" clearable></el-input>
        </el-form-item>
        <el-form-item label="工种 :">
          <el-select v-model="formChange.typeId" placeholder="请选择工种" style="width:200px;" clearable>
            <el-option label="副经理" value="1"></el-option>
            <el-option label="主管" value="2"></el-option>
            <el-option label="班长" value="3"></el-option>
            <el-option label="电工" value="4"></el-option>
            <el-option label="瓦工" value="5"></el-option>
            <el-option label="水工" value="6"></el-option>
            <el-option label="木工" value="7"></el-option>
            <el-option label="综合" value="8"></el-option>
            <el-option label="水电工" value="9"></el-option>
            <el-option label="水电木工" value="10"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="formChange.state" placeholder="请选择状态" style="width:200px;" clearable>
            <el-option label="在职" value="0"></el-option>
            <el-option label="离职" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateMaintainerConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改维修工弹窗 -->

  </div>
</template>

<script>
import { getMaintainerList, addMaintainer, updateMaintainer, deleteMaintainer, exportMaintainer } from '_api/UserManage/Maintainer/index.js'
import { getCampusName } from '_api/DormManage/Building/index.js'
import { getDepartmentName } from '_api/DormManage/Department/index.js'
import { encode } from '@utils/upload/base64.js'

export default {
  data() {
    return {
      labelPosition: "right",
      dialog_newly: false,
      dialog_change: false,
      form: {
        current: 0,
        deptId: "",
        field: "",
        name: "",
        order: "",
        pageSize: 10,
        typeId: ""
      },
      formNew: {
        campusId: "",
        deptId: "",
        password: "",
        phone: "",
        sex: "",
        typeId: "",
        workerName: "",
        workerNum: "",
        idLast: ""
      },
      formChange: {
        campusId: "",
        deptId: "",
        id: "",
        password: "",
        phone: "",
        sex: "",
        state: "",
        typeId: "",
        workerName: "",
        workerNum: "",
        idLast: ""
      },
      tableData: [],
      CampusName: [],
      DepartmentName: [],
      total: 0,
      timeNow: "",
    }
  },
  methods: {
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '/' + mm + '/' + dd + ' ' + hh + ':' + mf + ':' + ss;
      // console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    // 导出维修工信息
    exportMaintainer() {
      exportMaintainer(this.form).then(res => {
        // console.log(res.data,"121212121")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '维修工信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    handleSelectionChange() {

    },
    //重置
    reset() {
      this.form = {
        current: 0,
        deptId: "",
        field: "",
        name: "",
        order: "",
        pageSize: 10,
        typeId: ""
      }
      this.getMaintainerList()
    },
    //删除维修工
    deleteMaintainer(e) {
      this.$confirm('此操作将删除该维修工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMaintainer(e).then(res => {
          // console.log(res)
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.reset()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //修改维修工弹窗
    updateMaintainer(e) {
      this.formChange = {
        campusId: "",
        deptId: "",
        id: "",
        password: "",
        phone: "",
        sex: "",
        state: "",
        typeId: "",
        workerName: "",
        workerNum: "",
        idLast: ""
      }
      this.dialog_change = true
      this.DepartmentName.forEach(item => {
        if (item.departName == e.dept) {
          this.formChange.deptId = item.id
        }
      })
      this.CampusName.forEach(item => {
        if (item.name == e.campusName) {
          this.formChange.campusId = item.id
        }
      })
      this.formChange.id = e.id
      if (e.typeId) {
        this.formChange.typeId = e.typeId.toString()
      }
      this.formChange.phone = e.phone
      this.formChange.sex = e.sex
      if (e.state) {
        this.formChange.state = e.state.toString()
      }
      this.formChange.workerName = e.workerName
      this.formChange.workerNum = e.workerNum
      this.formChange.idLast = e.idLast

      // console.log(this.formChange)
    },
    //修改维修工确认
    updateMaintainerConfirm() {
      this.formChange.typeId = Number(this.formChange.typeId)
      var data = JSON.parse(JSON.stringify(this.formChange))
      if (data.password) {
        data.password = encode(data.password, 'UTF-8')
      }
      updateMaintainer(data).then(res => {
        if (res.status == 200) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.reset()
            }
          })
        }
      })
    },
    //新增维修工
    addMaintainer() {
      this.formNew.typeId = Number(this.formNew.typeId)
      var data = JSON.parse(JSON.stringify(this.formNew))
      data.password = encode(data.password, 'UTF-8')
      addMaintainer(data).then(res => {
        console.log(res)
        if (res.data.success == true) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.reset()
            }
          })
        } else {
          this.$message.info(res.data.message)
        }
      }).catch(error => {
        console.log(error.response)
        if (error.response.status != 200) {
          this.$message.info(error.response.data.message)
        }
      })
    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getMaintainerList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getMaintainerList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getMaintainerList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getMaintainerList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getMaintainerList()
    },
    //查询维修工列表
    getMaintainerList() {
      getMaintainerList(this.form).then(res => {
        console.log(res)
        if (res.data.success == true) {
          this.getCampusName()
          this.getDepartmentName()
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
    //查询校区名称
    getCampusName() {
      getCampusName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CampusName = res.data.data
        }
      })
    },
    //获取部门名称
    getDepartmentName() {
      getDepartmentName(
        { departType: 1 }
      ).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.DepartmentName = res.data.data
        }
      })
    }
  },
  created() {
    this.getMaintainerList()
    // this.getCampusName()
    // this.getDepartmentName()
  },
};
</script>
<style lang="scss" scoped>
.search_button {
  margin-left: 40px;
}

.reset_button {
  margin-left: 10px;
}

// .delete_button{
//   margin-left: 10px;
// }
// .dialogbutton{
//   margin-right: 40%;
// }
.pagination {
  float: right;
  margin-top: 10px;
}
</style>
